<?php
    /**
     * Created by PhpStorm.
     * User: lance
     * Date: 2017/11/1
     * Time: 16:36
     */

    namespace app\log\admin;


    use app\admin\controller\Admin;
    use think\Db;
    use app\common\builder\ZBuilder;
    /**
     *领导日志
     * Class Manager
     * @package app\log\admin
     */
    class Leader extends Common
    {
        public function showAddLog(){
            //领导能看所有客户经理添加日志
            $a_where = [
                'a.role_type' => 3,
                'a.action' => 1
            ];
            $a_field = [
                'department','name','sex','number','action','time','status','a.id id'
            ];
            $s_tips = <<<EOT
1.操作栏，可查看新增领导的操作人员、以及新增领导的信息(添加成功的领导会显示最新的领导信息，失败时只会显示最初添加领导的信息)
EOT;
            $a_where = $this->getMap() + $a_where;
            $a_log = Db::name('role_log a')->field($a_field)->join('manager b', 'a.role_id=b.id','LEFT')->where($a_where)->order('time desc')->paginate();
            return ZBuilder::make('table')
                ->hideCheckbox()
                ->setSearch([
                    'b.department' => '部门',
                    'b.name' => '姓名',
                    'b.number' => '柜员号',
                ])
                ->setPageTips($s_tips)
                ->addFilter('status',['失败','成功'],'','radio')
                ->setTableName('role_log')
                ->addTimeFilter('time')
                ->addColumns(config('leader_log'))
                ->addTrClass('danger','status',0)
                ->addTrClass('success','status',1)
                ->setRowList($a_log)
                ->addRightButton('custom',
                    [
                        'title' => '查看详情',
                        'icon' => 'glyphicon glyphicon-eye-open',
                        'href' => url('showadddetail', [
                            'id' => '__id__'
                        ]),
                    ], true)
                ->fetch();
        }
        public function showUpdateLog(){
            //领导能看所有客户经理添加日志
            $a_where = [
                'a.role_type' => 3,
                'a.action' => 2
            ];
            $a_field = [
                'department','name','sex','number','action','time','status','a.id id'
            ];
            $s_tips = <<<EOT
1.操作栏，可查看新增领导的操作人员、以及新增领导的信息(添加成功的领导会显示最新的领导信息，失败时只会显示最初添加领导的信息)
EOT;
            $a_where = $this->getMap() + $a_where;
            $a_log = Db::name('role_log a')->field($a_field)->join('manager b', 'a.role_id=b.id','LEFT')->where($a_where)->order('time desc')->paginate();
            return ZBuilder::make('table')
                ->hideCheckbox()
                ->setSearch([
                    'b.department' => '部门',
                    'b.name' => '姓名',
                    'b.number' => '柜员号',
                ])
                ->setPageTips($s_tips)
                ->addFilter('status',['失败','成功'],'','radio')
                ->setTableName('role_log')
                ->addTimeFilter('time')
                ->addColumns(config('leader_log'))
                ->addTrClass('danger','status',0)
                ->addTrClass('success','status',1)
                ->setRowList($a_log)
                ->addRightButton('custom',
                    [
                        'title' => '查看详情',
                        'icon' => 'glyphicon glyphicon-eye-open',
                        'href' => url('showadddetail', [
                            'id' => '__id__'
                        ]),
                    ], true)
                ->fetch();
        }
        /**
         * 客户经理新增日志
         */
        function showAddDetail(){
            $a_log = Db::name('role_log')->where('id', input('id/d', 0))->find();
            //描述
            $a_operate = Db::name('admin_user')->where('id', $a_log['operate_id'])->find();
            $a_role = Db::name('admin_role')->where('id', $a_operate['role'])->find();
            $a_content = json_decode($a_log['content'], true);
            //客户经理信息
            $a_leader = $a_log['status'] ? Db::name('manager')->where('id', $a_log['role_id'])->find() : $a_content;
            $i_id = $a_log['status']?$a_leader['id']:'';
            $a_log['status'] = $a_log['status'] ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>';
            $s_message = "操作时间：" . date('Y-m-d H:i:s', $a_log['time'])
                . '<br>状态：' . $a_log['status']
                . '<br>操作人员：' . $a_operate['username'] . '(' . $a_role['name'] . ')'
                . '<br>描述：' . config('leader_log')[5][4][$a_log['action']]
                . '<br>错误信息：' . $a_log['error'];
            $s_leader = 'ID：' . $i_id
                . '<br>部门：' . $a_leader['department'] . '<br>性别：' . $a_leader['sex']
                . '<br>姓名：' . $a_leader['name'] . '<br>性别：' . $a_leader['sex']
                . '<br>柜员号：' . $a_leader['number'] . '<br>证件号码：' . $a_leader['cert_number']
                . '<br>联系电话：'.$a_leader['tel_number']
                .'<br>办公室电话：'.$a_leader['office_number']
                .'<br>电子邮箱：'.$a_leader['email'];

            return $this->fetch('',
                [
                    'leader' => $s_leader,
                    'message' => $s_message
                ]);
        }
    }